var area_name = null;
var id = null;


var theData = new Vue({
    el: "#wrap",
    data: {
        nowPage: 1,   //当前页码
        totalPage: 0,  //总页码
        paginationItems: [   //页面数组
            /*{page:{}}*/  //数据格式
        ],
        tableItems: [       //表格数组
            /*{info:{}}*/  //数据格式
        ],
        setUp_user: {},     //配置的用户
        search_btn_remark: "",    //存放用户搜索内容
        token: token,        //input file 的加密
        time: unixtime       //input file 的加密
    },
    methods: {
        getList: function (nextPage) {   //获取每个分页的数据
            var that = this;
            var search = "";
            if (that.search_btn_remark != "") {
                search = that.search_btn_remark;
            }
            $.ajax({
                url: 'api/area/get_area_list.php',
                type: 'POST',
                data: {
                    token: token,
                    time: unixtime,
                    nextPage: nextPage,
                    search: search
                },
                dataType: 'JSON',
                beforeSend: function () {
                    $("#loading-mask").show();
                },
                error: function () {
                    $("#loading-mask").hide();
                    MyAlert("请求超时");
                },
                success: function (res) {
                    $("#loading-mask").hide();
                    if (res.err == 0){
                        console.log(res);
                        that.tableItems = res.msg;
                        that.nowPage = nextPage;
                    } else {
                        MyAlert(res.msg);
                    }
                }
            })
        },
        getPagination: function () {    //获取分页数量
            var that = this;
            var search = "";
            if (that.search_btn_remark != "") {
                search = that.search_btn_remark;
            }
            $.ajax({
                url: 'api/area/area_pag_info.php',
                type: 'POST',
                data: {
                    token: token,
                    time: unixtime,
                    search: search
                },
                dataType: 'JSON',
                beforeSend: function () {
                    $("#loading-mask").show();
                },
                error: function () {
                    $("#loading-mask").hide();
                    MyAlert("分页请求超时");
                },
                success: function (res) {
                    $("#loading-mask").hide();
                    if (res.err == 0) {
                        console.log(res.msg);
                        that.paginationItems = res.msg;
                        that.totalPage = res.total_page;
                    } else {
                        MyAlert(res.msg);
                    }
                }
            })
        },
        search: function () {      //查询
            var regSpace = /^\s+$/;
            if ($("#search-text").val() && !regSpace.test($("#search-text").val())) {
                this.search_btn_remark = $("#search-text").val();
                this.getPagination();
                this.getList(1);
            } else {
                MyAlert("请输入搜索内容");
            }
        },
        popAddArea: function () {      //添加区域弹窗
            $("#pop-add-area").modal();
        },
        popAddAreaSubmit: function () {//添加区域提交
            var name = $("#pop-add-area-name").val();
            console.log(name);
            if(name!=""){
                $.ajax({
                    url: 'api/area/add_area.php',
                    type: 'POST',
                    data: {
                        token: token,
                        time: unixtime,
                        area_name:name,
                    },
                    dataType: 'JSON',
                    beforeSend: function () {
                        $("#loading-mask").show();
                    },
                    error: function () {
                        $("#loading-mask").hide();
                    },
                    success: function (res) {
                        console.log(res);
                        $("#loading-mask").hide();
                        if (res.err == 0) {
                            MyAlert(res.msg, function () {
                                window.location.reload();
                            });

                        } else {
                            MyAlert(res.msg);
                        }
                    }
                })
            }
            else{
                MyAlert("请填写必填信息");
            }
        },
        popUpdataArea: function (item) {  //修改区域弹窗
            $("#pop-updata-area").modal();
            $("#pop-updata-area-name").val(item.info.area_name);
            area_name = item.info.area_name;
            id= item.info.id;

        },

        popUpdataAreaSubmit:function () {//修改区域信息
            var name = $("#pop-updata-area-name").val();
            if(name==area_name){
                MyAlert("信息未修改！");
            }
            else if(name==""){
                MyAlert("请填写修改信息！");
            }
            else{
                MyConfirm("确定修改?", function (){
                    $.ajax({
                        url: "api/area/updataAreaAuthority.php",
                        type: 'POST',
                        data: {
                            token: token,
                            time: unixtime,
                            area_name:name,
                            id:id,
                        },
                        dataType: 'JSON',
                        beforeSend: function () {
                            $("#loading-mask").show();
                        },
                        error: function () {
                            $("#loading-mask").hide();
                            alert("请求超时");
                        },
                        success: function (res) {
                            $("#loading-mask").hide();
                            if (res.err == 0) {
                                MyAlert(res.msg, function () {
                                    console.log(123);
                                    window.location.reload();
                                });
                            } else {
                                MyAlert(res.msg);
                            }
                        }
                    })
                })
            }
        },
        deleteAreaAuthority: function (index) {   //删除用户信息
            console.log(index);
            MyConfirm("确定删除?", function () {
                $.ajax({
                    url: "api/area/deleteAreaInfo.php",
                    type: 'POST',
                    data: {
                        token: token,
                        time: unixtime,
                        index: index
                    },
                    dataType: 'JSON',
                    beforeSend: function () {
                        $("#loading-mask").show();
                    },
                    error: function () {
                        $("#loading-mask").hide();
                        alert("请求超时");
                    },
                    success: function (res) {
                        $("#loading-mask").hide();
                        if (res.err == 0) {
                            MyAlert(res.msg, function () {
                                console.log(123);
                                window.location.reload();
                            });
                        } else {
                            MyAlert(res.msg);
                        }
                    }
                })
            })
        },


    }
});

theData.getPagination();   //首次加载总页码
theData.getList(theData.nowPage);  //首次加载首个分页